﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.DataVisualization.Charting;

namespace WebReport.Reports
{
    public partial class WebPerfomance : System.Web.UI.Page
    {
        private const int PageSize = 10;
        protected Dictionary<string, string> dicUrl = new Dictionary<string, string>();

        protected void Page_Load(object sender, EventArgs e)
        {
            Pager.PageChange += new PagerControl.PageRefresh(Pager_PageChange);
            InitChart();
            BindChart();
            if (!Page.IsPostBack)
            {
                BindGrid(0);
            }
        }

        void Pager_PageChange(int pageIndex)
        {
            BindGrid(pageIndex);
        }
        protected void InitChart()
        {
            SplineChart.Titles.Add("Web Performance Chart");
            //SplineChart
        }
        protected void BindChart()
        {
            using (DefaultDBEntity db = new DefaultDBEntity())
            {
                var query = from p in db.HtmlAction
                            select p.Url;
                var urlList = query.Distinct().ToList();
                int i = 1;
                foreach (var url in urlList)
                {
                    string name = "Scenario " + i.ToString();
                    dicUrl.Add(url, name);
                    BindChartByUrl(url,name);
                    i++;
                }
            }
           
        }
        protected void BindChartByUrl(string url,string name)
        {
            using (DefaultDBEntity db = new DefaultDBEntity())
            {
                var query = from p in db.HtmlAction
                            //where p.Url == url
                            orderby p.BeginTime
                            group p by p.BeginTimeFormat into g
                            select new
                            {
                                BeginTime = g.Key,
                                Url = name,
                                Duration = g.Average(t => t.ElapsedTime)
                            };
                var list = query.ToList();

                Series s = new Series(name);
                s.ChartType = SeriesChartType.Spline;
                s.BorderWidth = 2;
                //s.ShadowOffset = 3;
                s.IsVisibleInLegend = true;
                foreach (var item in list)
                {
                    DataPoint dp = new DataPoint();
                    dp.AxisLabel = item.BeginTime.Value.ToString("yyyy/MM/dd HH:mm");
                    dp.SetValueY(item.Duration.Value);
                    s.Points.Add(dp);
                }
                SplineChart.Series.Add(s);
            }
        }
        protected void BindGrid(int pageIndex)
        {
            using (DefaultDBEntity db = new DefaultDBEntity())
            {
                var query = from p in db.HtmlAction
                            orderby p.ActionID
                            select p;
                            //select new 
                            //{
                            //    p.ActionID,
                            //    Url=dicUrl[p.Url],
                            //    p.ElapsedTime,
                            //    p.BeginTime,
                            //    p.EndTime,
                            //    p.Location
                            //};
                var list = query.Skip(pageIndex*PageSize).Take(PageSize).ToList();
                gvAction.DataSource = list;
                gvAction.DataBind();

                Pager.IntialProperties(query.Count());
                Pager.CurrentPageIndex = pageIndex;
            }
        }

        protected void gvAction_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var item = e.Row.DataItem as HtmlAction;
                if (item != null)
                {
                    HyperLink btnDetail = e.Row.Cells[6].FindControl("btnDetail") as HyperLink;
                    btnDetail.NavigateUrl = string.Format("RequestDetail.aspx?btime={0}&etime={1}&keepThis=true&TB_iframe=true&height=500&width=1000",item.BeginTime, item.EndTime);
                }

                string url = e.Row.Cells[1].Text;
                e.Row.Cells[1].Text = dicUrl[url];
            }
        }

        protected void gvAction_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            BindGrid(e.NewPageIndex);
        }

    }
}